package com.taiji.workOrder.Utils;

import com.taiji.common.exception.file.FileNameLengthLimitExceededException;
import com.taiji.common.exception.file.FileSizeLimitExceededException;
import com.taiji.common.exception.file.InvalidExtensionException;
import com.taiji.common.utils.file.FileUploadUtils;
import com.taiji.common.utils.file.MimeTypeUtils;
import com.taiji.system.service.impl.SysDictDataServiceImpl;
import com.taiji.workOrder.domain.WorkOrder;
import com.taiji.workOrder.mapper.WorkOrderMapper;
import com.taiji.workOrder.service.impl.WorkOrderServiceImpl;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;

import static com.taiji.common.utils.file.FileUploadUtils.*;

@Component
public class WorkOrderFileUtil {

    @Resource
    private  SysDictDataServiceImpl dictDataService;

    private static SysDictDataServiceImpl dictDataServiceStatic;


    @Resource
    private  WorkOrderServiceImpl workOrderService;

    private static WorkOrderServiceImpl workOrderServiceStatic;

    @PostConstruct
    public void initialize(){
        dictDataServiceStatic=this.dictDataService;
    }

    @PostConstruct
    public void init(){
        workOrderServiceStatic=this.workOrderService;
    }

    /**
     * 工单上传附件
     *
     * @param baseDir 相对应用的基目录
     * @param file 上传的文件
     * @return 文件名称
     * @throws IOException
     */
    public static final String uploadWorkOrderFile(String baseDir, MultipartFile file, String workNumber,String projectCode) throws IOException
    {
        try
        {
            return uploadWorkOrderFile(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION,workNumber,projectCode);
        }
        catch (Exception e)
        {
            throw new IOException(e.getMessage(), e);
        }
    }


    /**
     * 工单上传附件
     *
     * @param baseDir 相对应用的基目录
     * @param file 上传的文件
     * @param allowedExtension 上传文件类型
     * @return 返回上传成功的文件名
     * @throws FileSizeLimitExceededException 如果超出最大大小
     * @throws FileNameLengthLimitExceededException 文件名太长
     * @throws IOException 比如读写文件出错时
     * @throws InvalidExtensionException 文件校验异常
     */
    public static String uploadWorkOrderFile(String baseDir, MultipartFile file, String[] allowedExtension, String workNumber,String projectCode)
            throws Exception {
//        WorkOrder workOrder = workOrderServiceStatic.selectWorkOrderByWorkNumber(workNumber);
        String rand = workNumber.substring(workNumber.length() - 4, workNumber.length());
        String bmgz = projectCode.concat("_").concat("bmgz");
        String fj = dictDataServiceStatic.selectDictValue(projectCode, bmgz, "附件");
        if (null==fj){
            throw new Exception("该项目尚未在字典中配置工单附件，请配置后再上传文件");
        }
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        String fileName=projectCode.concat("-").concat(fj).concat("-").concat(rand).concat("-")
                .concat(ChineseTextToPinyin.ramdomNumber()).concat(".").concat(extension);
        int fileNamelength = Objects.requireNonNull(file.getOriginalFilename()).length();
        if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
        {
            throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
        }

        assertAllowed(file, allowedExtension);
        //编码文件名称
//        String fileName = extractFilename(file);

        String absPath = getAbsoluteFile(baseDir, fileName).getAbsolutePath();
        file.transferTo(Paths.get(absPath));
        return getPathFileName(baseDir, fileName);
    }


    /**
     * 报告上传
     *
     * @param baseDir 相对应用的基目录
     * @param file 上传的文件
     * @return 文件名称
     * @throws IOException
     */
    public static final String uploadReportFile(String baseDir, MultipartFile file,String projectCode) throws IOException
    {
        try
        {
            return uploadReportFile(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION,projectCode);
        }
        catch (Exception e)
        {
            throw new IOException(e.getMessage(), e);
        }
    }

    /**
     * 报告上传
     *
     * @param baseDir 相对应用的基目录
     * @param file 上传的文件
     * @param allowedExtension 上传文件类型
     * @return 返回上传成功的文件名
     * @throws FileSizeLimitExceededException 如果超出最大大小
     * @throws FileNameLengthLimitExceededException 文件名太长
     * @throws IOException 比如读写文件出错时
     * @throws InvalidExtensionException 文件校验异常
     */
    public static String uploadReportFile(String baseDir, MultipartFile file, String[] allowedExtension,String projectCode)
            throws Exception {
        String bmgz = projectCode.concat("_").concat("bmgz");
        String fj = dictDataServiceStatic.selectDictValue(projectCode, bmgz, "报告");
        if (null==fj){
            throw new Exception("该项目尚未在字典中配置报告，请配置后再上传文件");
        }
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        String fileName=projectCode.concat("-").concat(fj).concat("-")
                .concat(ChineseTextToPinyin.ramdomNumber()).concat(".").concat(extension);
        int fileNamelength = Objects.requireNonNull(file.getOriginalFilename()).length();
        if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
        {
            throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
        }

        assertAllowed(file, allowedExtension);
        //编码文件名称
//        String fileName = extractFilename(file);

        String absPath = getAbsoluteFile(baseDir, fileName).getAbsolutePath();
        file.transferTo(Paths.get(absPath));
        return getPathFileName(baseDir, fileName);
    }

}
